package com.usabb.utils;

import com.usabb.api.dto.AmenitiesDTO;
import com.usabb.api.dto.AmenityDTO;
import com.usabb.domain.AmenityDO;
import com.usabb.enums.AmenityEnum;
import org.springframework.ui.Model;

import java.util.List;
import java.util.stream.Collectors;

public final class AmenityUtils {

    public static List<AmenityDO> extractCommunity(final List<AmenityDO> amenityDOS) {
        return amenityDOS.stream()
                .filter(it -> AmenityEnum.isCommunity(it))
                .collect(Collectors.toList());
    }

    public static List<AmenityDO> extractVillaFacility(final List<AmenityDO> amenityDOS) {
        return amenityDOS.stream()
                .filter(it -> AmenityEnum.isVillaFacility(it))
                .collect(Collectors.toList());
    }

    public static List<AmenityDO> extractRoomFacility(final List<AmenityDO> amenityDOS) {
        return amenityDOS.stream()
                .filter(it -> AmenityEnum.isRoomFacility(it))
                .collect(Collectors.toList());
    }

    public static List<AmenityDO> extractBabyRoomList(final List<AmenityDO> amenityDOS) {
        return amenityDOS.stream()
                .filter(it -> AmenityEnum.isBabyRoom(it))
                .collect(Collectors.toList());
    }

    public static AmenitiesDTO extractTo(final List<AmenityDO> amenityDOS) {
        final List<AmenityDO> communityList = AmenityUtils.extractCommunity(amenityDOS);
        final List<AmenityDO> villaFacilityList = AmenityUtils.extractVillaFacility(amenityDOS);
        final List<AmenityDO> roomFacilityList = AmenityUtils.extractRoomFacility(amenityDOS);
        final List<AmenityDO> babyRoomList = AmenityUtils.extractBabyRoomList(amenityDOS);

        AmenitiesDTO dto = new AmenitiesDTO();
        dto.setCommunityList(transform(communityList));
        dto.setVillaFacilityList(transform(villaFacilityList));
        dto.setRoomFacilityList(transform(roomFacilityList));
        dto.setBabyRoomList(transform(babyRoomList));

        return dto;
    }

    public static List<AmenityDTO> transform(List<AmenityDO> doList) {
        return doList.stream().map(it -> AmenityDTO.of(it)).collect(Collectors.toList());
    }

    /**
     *  model.addAttribute("communityList", communityList);
     * 	model.addAttribute("villaFacilityList", villaFacilityList);
     * 	model.addAttribute("roomFacilityList", roomFacilityList);
     * 	model.addAttribute("babyRoomList", babyRoomList);
     */
    public static void append(Model model, final List<AmenityDO> amenityDOS) {
        AmenitiesDTO dto = extractTo(amenityDOS);

        model.addAttribute("communityList", dto.getCommunityList());
        model.addAttribute("villaFacilityList", dto.getVillaFacilityList());
        model.addAttribute("roomFacilityList", dto.getRoomFacilityList());
        model.addAttribute("babyRoomList", dto.getBabyRoomList());

    }


}
